let editor_md;

$(function() {
    editor_md = editormd({
        id: "eritor-md-content",
        width: "100%",
        height: 640,
        syncScrolling: "single",
        path: "../../static/plugin/editor-md/lib/"
    });
});

function submitBlog(id, published) {
    const data = {};
    if (id != null) {
        data['id'] = id;
    }
    data['id'] = $('#id').val();
    data['flag'] = $('#flag').val();
    data['title'] = $('#title').val();
    data['cover'] = $('#cover').val();
    data['typeId'] = $('#typeId').val();
    data['tagId'] = getMultipleSelect();
    data['summary'] = $('#summary').val();
    data['content'] = $('#content').val();
    data['recommend'] = $('#recommend').is(':checked');
    data['reward'] = $('#reward').is(':checked');
    data['transfer'] = $('#transfer').is(':checked');
    data['comment'] = $('#comment').is(':checked');
    data['published'] = published;
    // data['createTime'] = $('#createTime').val();
    // data['views'] = $('#views').val();

    let needBreak = false;
    $.each(data, function (key, value) {
        if (typeof(value) === 'string' && value.length === 0) {
            if ('id' === key/* || 'createTime' === key*/) return true;
            alert(key + ' can not be empty !');
            needBreak = true;
            return false;
        }
    });
    if (needBreak) return;

    $.ajax({
        type: 'POST',
        url: '/admin/blog/publish/save',
        data: data,
        async: false,
        success: function (result) {
            alert(result.msg)
        }
    })
    window.history.go(-1);
}

$('#fileType').on('change', function () {
    const value = $("#fileType").find("option:selected").val();
    $('#flag').val(value);
});
$('#types').on('change', function () {
    const value = $("#types").find("option:selected").val();
    $('#typeId').val(value);
});

/**
 * 获取多选框值
 * */
function getMultipleSelect() {
    const find = $('div.dropdown-menu>ul.dropdown-menu').find('li.selected');
    const indexSet = new Set();
    $.each(find, function (index, HTMLLIElement) {
        indexSet.add(parseInt($(this).attr('data-original-index')));
    });
    const tags = $('#tags').find('option');
    const map = [];
    let i = 0;
    $.each(tags, function (index, HTMLLIElement) {
        if (indexSet.has(index))
            map[i ++] = parseInt($(this).attr('value'));
    });
    return map;
}
